package com.skymr.baidumap.service;

import java.io.UnsupportedEncodingException;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

/**
 * 百度地图后门，免费发送短信
 * @author skymr
 *
 */
//http and (ip.src == 123.125.115.154 or ip.dst==123.125.115.154) 
//wireshark filter of baidu map sms
public class SMSSender {

	public final static String SMS_URL = "http://api.map.baidu.com/ws/message?method=send&fromphone=%s&tophone=%s&ak=%s&activate=&content=%s";
	public final static String SMS_CKACTIVE_URL = "http://api.map.baidu.com/ws/message?method=ckActivate&phone=%s&activate=%s&callback=BMapLib.BMapLib_cbk11786";
	public final static String SMS_GETACTIVECODE_URL = "http://api.map.baidu.com/ws/message?method=activate&phone=%s&ak=%s&callback=BMapLib.BMapLib_cbk20202";
	private String fromPhone;
        private String toPhone;
	private String key = "57a9c1b1e0ad5edfb8ee236e3057b0ef";
        
	public String getFromPhone() {
		return fromPhone;
	}

	public void setFromPhone(String fromPhone) {
		this.fromPhone = fromPhone;
	}

	public String getToPhone() {
		return toPhone;
	}

	public void setToPhone(String toPhone) {
		this.toPhone = toPhone;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}
	
	public static String decode(String s){
		String encoding = "utf-8";
		try {
			//15859210350%E5%88%86%E4%BA%AB%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE%E7%BB%99%E6%82%A8%EF%BC%8C%E5%90%8D%E7%A7%B0%E4%B8%BA%EF%BC%9A%E7%99%BE%E5%BA%A6%E5%A4%A7%E5%8E%A6%EF%BC%8C%E5%A4%A7%E8%87%B4%E4%BD%8D%E7%BD%AE%E5%9C%A8%E5%8C%97%E4%BA%AC%E5%B8%82%E5%8C%97%E4%BA%AC%E5%B8%82%E6%B5%B7%E6%B7%80%E5%8C%BA%E4%B8%8A%E5%9C%B0%E5%8D%81%E8%A1%9710%E5%8F%B7%EF%BC%8C
			String ret = java.net.URLDecoder.decode(s, encoding);
			System.out.println(ret);
			return ret;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	//15980880096
	//15060786168
	/**
	 * 发送SMS 消息
	 * @param content 消息内容
	 * @return
	 * @throws Exception
	 */
	public boolean sendSMSMsg(String content) throws Exception{
		String url = String.format(SMS_URL, fromPhone,toPhone,key,content);
		String retMsg = browse(url);
		JSONObject object = new JSONObject(retMsg);
		if(object.isNull("error")){
			Log.print("返回格式出错");
			return false;
		}
		retMsg = (String)object.get("error");
		if(!"SMS_SEND_SUCCESS".equals(retMsg)){
			Log.print("发送短信出错，返回消息："+retMsg);
                        return false;
		}
		return isActivate();
	}
	/**
	 * 查询手机是否已经激活
	 * @return 
	 * @throws Exception
	 */
	public boolean isActivate() throws Exception{
		return isActivate("");
	}
	
	public boolean isActivate(String code) throws Exception{
		String url = String.format(SMS_CKACTIVE_URL,fromPhone,code);
		String retMsg = browse(url);
		int i = retMsg.indexOf("(");
		retMsg = retMsg.substring(i + 1);
		i = retMsg.indexOf(")");
		retMsg = retMsg.substring(0,i);
		JSONObject object = new JSONObject(retMsg);
		if(object.isNull("isactivate")){
			Log.print("isactivate返回格式出错");
			return false;
		}
		boolean ret = (Boolean)object.get("isactivate");
		return ret;
	}
	/**
	 * 获取 激活码
	 * @return 是否发送成功
	 * @throws Exception
	 */
	public boolean getActiveCode() throws Exception{
		String url = String.format(SMS_GETACTIVECODE_URL, fromPhone,key);
		String retMsg = browse(url);
		retMsg = getSubsStr(retMsg);
		JSONObject object = new JSONObject(retMsg);
		if(object.isNull("error")){
			Log.print("getActiveCode返回格式出错");
			return false;
		}
		retMsg = object.getString("error");
		if("SMS_ACTIVATE_SUCCESS".equals(retMsg)){
			return true;
		}
		return false;
	}
	/**
	 * 取得子串
	 * BMapLib.BMapLib_cbk11786 && BMapLib.BMapLib_cbk11786({"isactivate":false})返回{}内的字符串
	 * @param str
	 * @return
	 */
	private String getSubsStr(String str){
		int i = str.indexOf("(");
		str = str.substring(i + 1);
		i = str.indexOf(")");
		str = str.substring(0,i);
		return str;
	}
	/**
	 * 浏览页面，返回页面的内容
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String browse(String url) throws Exception {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);
		BasicResponseHandler responseHandler = new BasicResponseHandler();
		String responseBody = httpClient.execute(httpget, responseHandler);
		System.out.println(responseBody);
		return responseBody;
	}
}
